<?php
class BBCode
{
    // Send all text to be parsed here!
	function coverpass($text)
	{
		return $this->parsetags($text); // Yup, Parse Them
	}
	
	// based on http://www.phpit.net/article/create-bbcode-php/  
	// modified by www.vision.to  
	// please keep credits, thank you :-)  
	// document your changes.  
	  
	function parsetags ($str) {  
		
		$simple_search = array(  
					//added line break  
					'/\[br\]/is',  
					'/\[b\](.*?)\[\/b\]/is',  
					'/\[i\](.*?)\[\/i\]/is',  
					'/\[u\](.*?)\[\/u\]/is',  
					'/\[url\=(.*?)\](.*?)\[\/url\]/is',  
					'/\[url\](.*?)\[\/url\]/is',
					'/\[left\](.*?)\[\/left\]/',
					'/\[center\](.*?)\[\/center\]/',
					'/\[right\](.*?)\[\/right\]/',
					'/\[align\=(left|center|right)\](.*?)\[\/align\]/is',  
					'/\[img\](.*?)\[\/img\]/is',  
					'/\[mail\=(.*?)\](.*?)\[\/mail\]/is',  
					'/\[mail\](.*?)\[\/mail\]/is',  
					'/\[font\=(.*?)\](.*?)\[\/font\]/is',  
					'/\[size\=(.*?)\](.*?)\[\/size\]/is',  
					'/\[color\=(.*?)\](.*?)\[\/color\]/is',  
					//added textarea for code presentation  
					'/\[codearea\](.*?)\[\/codearea\]/is',  
					//added pre class for code presentation  
					'/\[code\](.*?)\[\/code\]/is',  
					//added paragraph  
					'/\[p\](.*?)\[\/p\]/is',
					'/\[si\=(.*?)\]/is',
					'/\[spoil\](.*?)\[\/spoil\]/is'
				);  
	  
		$simple_replace = array(  
					//added line break  
				   '<br />',  
					'<strong>$1</strong>',  
					'<em>$1</em>',  
					'<u>$1</u>',  
					// added nofollow to prevent spam  
					'<a href="$1" rel="nofollow" title="$2 - $1" target="_blank">$2</a>',  
					'<a href="$1" rel="nofollow" title="$1" target="_blank">$1</a>',
					'<left>$1</left>',
					'<center>$1</center>',
					'<right>$1</right>',
					'<div style="text-align: $1;">$2</div>',  
					//added alt attribute for validation  
					'<img src="$1" alt="" onload="NcodeImageResizer.createOn(this);" />',
					'<a href="mailto:$1">$2</a>',  
					'<a href="mailto:$1">$1</a>',  
					'<span style="font-family: $1;">$2</span>',  
					'<span style="font-size: $1;">$2</span>',  
					'<span style="color: $1;">$2</span>',  
					//added textarea for code presentation  
					'<textarea class="code_container" rows="30" cols="70">$1</textarea>',  
					//added pre class for code presentation  
					'<pre><code>$1</code></pre>',
					//added paragraph  
					'<p>$1</p>',
					'<b style="cursor:pointer;" onmousedown="this.nextSibling.style.display=\'\';this.style.display=\'none\';">กรุณาคลิกที่นี่ เพื่อแสดงรูปภาพที่ซ่อนอยู่</b><img src="$1" style="display:none;cursor:pointer;" title="คลิกที่นี่เพื่อซ่อนรูปภาพ"  onmousedown="this.previousSibling.style.display=\'\';this.style.display=\'none\';" onload="NcodeImageResizer.createOn(this);" />',
					'<b style="cursor:pointer;" onmousedown="this.nextSibling.style.display=\'\';this.style.display=\'none\';">กรุณาคลิกที่นี่ เพื่อแสดงข้อมูลที่ซ่อนอยู่</b><span style="display:none;">$1</span>'
				);  
		
		$str = preg_replace_callback('~\[t=(\d+)\]~', 'zone_it_topic_bbcode_tag', $str);
		$str = preg_replace_callback('~\[u=(\d+)\]~', 'coms_post_user_bbcode_tag', $str);
		
		// Do simple BBCode's  
		$str = preg_replace ($simple_search, $simple_replace, $str);

		$str = preg_replace_callback('/\[youtube\](.*?)\[\/youtube\]/is', create_function('$m', '
			preg_match(\'/youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)[&\w;=\+_\-]*/\', $m[1], $matches);
			if(empty($matches)) return $m[0];
			$video_id = $matches[1];
			return \'<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/\'.$video_id.\'&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/\'.$video_id.\'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>\';
		'), $str);
	  
		// Do <blockquote> BBCode  
		$str = $this->bbcode_quote ($str);

		// Do emoticons
		$str = $this->parseemo ($str);
		
		$str = nl2br($str);
		
		return $str;  
	}
	
	function bbcode_quote ($str) {  
		//added p for quotes  
		$open = '<blockquote><p>';  
		$close = '</p></blockquote>';  
	  
		// How often is the open tag?  
		preg_match_all ('/\[quote\]/i', $str, $matches);  
		$opentags = count($matches['0']);  
	  
		// How often is the close tag?  
		preg_match_all ('/\[\/quote\]/i', $str, $matches);  
		$closetags = count($matches['0']);  
	  
		// Check how many tags have been unclosed  
		// And add the unclosing tag at the end of the message  
		$unclosed = $opentags - $closetags;  
		for ($i = 0; $i < $unclosed; $i++) {  
			$str .= '</p></blockquote>';  
		}  
	  
		// Do replacement  
		$str = str_replace ('[' . 'quote]', $open, $str);  
		$str = str_replace ('[/' . 'quote]', $close, $str);  
	  
		return $str;  
	}
	
	function getBBCode() {
		$bbcode = array(  
			array(
				'tag' => 'br',
				'before' => '[br]',
				'after' => ''
			),
			array(
				'tag' => 'b',
				'before' => '[b]',
				'after' => '[/b]'
			),
			array(
				'tag' => 'i',
				'before' => '[i]',
				'after' => '[/i]'
			),
			array(
				'tag' => 'u',
				'before' => '[u]',
				'after' => '[/u]'
			),
			 array(
				'tag' => 'url',
				'before' => '[url]',
				'after' => '[/url]'
			),
			array(
				'tag' => 'img',
				'before' => '[img]',
				'after' => '[/img]'
			)
		);
		
		return $bbcode;
	}
	
	function getEmos() {
		return array_combine($this->getEmoText(), $this->getEmoImage());
	}
	
	function getEmoText() {
		return array(
			':em01:',
			':em02:',
			':em03:',
			':em04:',
			':em05:',
			':em06:',
			':em07:',
			':em08:',
			':em09:',
			':em10:',
			':em11:',
			':em12:',
			':em13:',
			':em14:',
			':em15:',
			':em16:',
			':em17:',
			':em18:',
			':em19:',
			':em20:',
			':em21:',
			':em22:',
			':em23:',
			':em24:',
			':em25:',
			':em26:',
			':em27:',
			':em28:',
			':em29:',
			':em30:',
			':em31:',
			':em32:',
			':em33:'
		);
	}
	
	function getEmoImage () {
		return array(
			'<img src="'.EMO_URL.'/em01.gif" />',
			'<img src="'.EMO_URL.'/em02.gif" />',
			'<img src="'.EMO_URL.'/em03.gif" />',
			'<img src="'.EMO_URL.'/em04.gif" />',
			'<img src="'.EMO_URL.'/em05.gif" />',
			'<img src="'.EMO_URL.'/em06.gif" />',
			'<img src="'.EMO_URL.'/em07.gif" />',
			'<img src="'.EMO_URL.'/em08.gif" />',
			'<img src="'.EMO_URL.'/em09.gif" />',
			'<img src="'.EMO_URL.'/em10.gif" />',
			'<img src="'.EMO_URL.'/em11.gif" />',
			'<img src="'.EMO_URL.'/em12.gif" />',
			'<img src="'.EMO_URL.'/em13.gif" />',
			'<img src="'.EMO_URL.'/em14.gif" />',
			'<img src="'.EMO_URL.'/em15.gif" />',
			'<img src="'.EMO_URL.'/em16.gif" />',
			'<img src="'.EMO_URL.'/em17.gif" />',
			'<img src="'.EMO_URL.'/em18.gif" />',
			'<img src="'.EMO_URL.'/em19.gif" />',
			'<img src="'.EMO_URL.'/em20.gif" />',
			'<img src="'.EMO_URL.'/em21.gif" />',
			'<img src="'.EMO_URL.'/em22.gif" />',
			'<img src="'.EMO_URL.'/em23.gif" />',
			'<img src="'.EMO_URL.'/em24.gif" />',
			'<img src="'.EMO_URL.'/em25.gif" />',
			'<img src="'.EMO_URL.'/em26.gif" />',
			'<img src="'.EMO_URL.'/em27.gif" />',
			'<img src="'.EMO_URL.'/em28.gif" />',
			'<img src="'.EMO_URL.'/em29.gif" />',
			'<img src="'.EMO_URL.'/em30.gif" />',
			'<img src="'.EMO_URL.'/em31.gif" />',
			'<img src="'.EMO_URL.'/em32.gif" />',
			'<img src="'.EMO_URL.'/em33.gif" />'
		);
	}
	
	function parseemo ($str) {
		$emotext = $this->getEmoText();
		$emoimage = $this->getEmoImage();
		$str = str_replace ($emotext, $emoimage, $str);
		return $str;
	}
}
?>